### This script contains code for reproducing the results in:
### "Anti-Black Political Violence and the Historical Legacy of the Great Replacement Conspiracy"
### Perspectives on Politics

library(tidyverse)
library(patchwork)
library(jtools)
library(modelsummary)

#### INCLUDE CLEANING HERE

diversification_yougov <- read_csv("data/raw/diversification_yougov.csv", guess_max = 5000)
chronic_racial_threat_lucid <- read_csv("data/raw/chronic_racial_threat_lucid_anonymized.csv")
norc_diversification <- readxl::read_excel("data/raw/norc_diversification_priming_cleaned_w_exp.xlsx")


# Data Cleaning -----------------------------------------------------------


norc_diversification$demographic_prime <- ifelse(norc_diversification$diversification_experiment == "0", 0, 1)

# this function wraps scales::rescale to rescale items into the interval [0, 1].
# many of the vendors use out-of-range values, so we pass the valid range manually,
# then re-set the out-of-range values by seeing if they lie outside [0, 1].
# this requires us to say what the valid range is (e.g. c(1, 7) for a Likert scale)
rescale_numeric <- function(x, r) {
  rescaled = scales::rescale(x, from = r)
  rescaled = if_else(rescaled < 0 | rescaled > 1, NA, rescaled)
  return(rescaled)
}

norc_diversification <- norc_diversification |>
  mutate(
    PID = rescale_numeric(PartyID7, c(1, 7)),
    reasonable_violence = rescale_numeric(reasonable_to_use_violence, c(1, 5)),
    prepare_weapons = rescale_numeric(citizens_use_weapons, c(1, 5)),
    gender = rescale_numeric(GENDER, c(1, 2)),
    age = rescale_numeric(AGE, c(1, 7)),
    white_non_white = if_else(RACETHNICITY == 1, 1, 0),
    EDU = rescale_numeric(EDUC5, c(1, 5)),
    married = if_else(MARITAL == 1, 1, 0),
    employ = if_else(MARITAL %in% 1:2, 1, 0),
    income = rescale_numeric(INCOME, c(1, 18)),
    SOUTH = if_else(REGION4 == 3, 1, 0),
  )

norc_diversification_whites <- filter(norc_diversification, white_non_white == 1)

chronic_racial_threat_lucid <- chronic_racial_threat_lucid |>
  mutate(
    demo_prime = if_else(random1 == 0, 0, 1),
    black_present_threat_pre = rescale_numeric(present_threat_pre_1, c(1, 4)),
    asian_present_threat_pre = rescale_numeric(present_threat_pre_2, c(1, 4)),
    latinx_present_threat_pre = rescale_numeric(present_threat_pre_3, c(1, 4)),
    black_future_threat_pre = rescale_numeric(future_threat_pre_1, c(1, 4)),
    asian_future_threat_pre = rescale_numeric(future_threat_pre_2, c(1, 4)),
    latinx_future_threat_pre = rescale_numeric(future_threat_pre_3, c(1, 4)),
    violence_against_opposition_pre = rescale_numeric(anti_dem_pre_1, c(7, 1)),
    violence_against_opposition_post = rescale_numeric(anti_dem_post_1, c(7, 1)),
    #slavery_made_hard_for_Blacks_reverse_pre = rescale_numeric(rr_pre_3, c(7, 1)),
    #slavery_made_hard_for_Blacks_reverse_post = rescale_numeric(rr_post_3, c(7, 1)),
    slavery_made_hard_for_Blacks_reverse_pre = rescale_numeric(rr_pre_3, c(1, 7)),
    slavery_made_hard_for_Blacks_reverse_post = rescale_numeric(rr_post_3, c(1, 7)),
    ASIAN_THREAT = (asian_future_threat_pre + asian_present_threat_pre) / 2,
    LATINX_THREAT = (latinx_future_threat_pre + latinx_present_threat_pre) / 2,
    BLACK_THREAT = (black_future_threat_pre + black_present_threat_pre) / 2,
    PID = rescale_numeric(pid, c(1, 7)),
    IDEO = rescale_numeric(ideo, c(1, 7)),
    EDU = rescale_numeric(edu, c(1, 8)),
    REGION = factor(region_pre, levels = 1:4, labels = c("Midwest", "Northeast", "West", "South")),
    FEMALE = if_else(gender_pre == 3, NA, gender_pre - 1),
    AGE = scales::rescale(age)
  )

chronic_threat_LU_white<- filter(chronic_racial_threat_lucid, race == "White")


#### YOUGOV

diversification_yougov$diversification_treatment[diversification_yougov$Q18treat=="Hide"]=0
diversification_yougov$diversification_treatment[diversification_yougov$Q18treat=="Show"]=1


diversification_yougov$VOTER_REGISTERED_ALL[diversification_yougov$votereg=="Yes"]=1
diversification_yougov$VOTER_REGISTERED_ALL[diversification_yougov$votereg=="No"]=0
diversification_yougov$VOTER_REGISTERED_ALL[diversification_yougov$votereg=="Don't know"]=0


diversification_yougov$GENDER[diversification_yougov$gender=="Male"]=0
diversification_yougov$GENDER[diversification_yougov$gender=="Female"]=1

diversification_yougov$EDU[diversification_yougov$educ=="No HS"]=0
diversification_yougov$EDU[diversification_yougov$educ=="High school graduate"]=1/5
diversification_yougov$EDU[diversification_yougov$educ=="Some college"]=2/5
diversification_yougov$EDU[diversification_yougov$educ=="2-year"]=3/5
diversification_yougov$EDU[diversification_yougov$educ=="4-year"]=4/5
diversification_yougov$EDU[diversification_yougov$educ=="Post-grad"]=1

diversification_yougov$PID[diversification_yougov$pid7=="Strong Democrat"]=0
diversification_yougov$PID[diversification_yougov$pid7=="Not very strong Democrat"]=1/6
diversification_yougov$PID[diversification_yougov$pid7=="Lean Democrat"]=2/6
diversification_yougov$PID[diversification_yougov$pid7=="Independent"]=3/6
diversification_yougov$PID[diversification_yougov$pid7=="Lean Republican"]=4/6
diversification_yougov$PID[diversification_yougov$pid7=="Not very strong Republican"]=5/6
diversification_yougov$PID[diversification_yougov$pid7=="Strong Republican"]=1



diversification_yougov$white_nonwhite[diversification_yougov$race=="Asian"]=0
diversification_yougov$white_nonwhite[diversification_yougov$race=="Black"]=0
diversification_yougov$white_nonwhite[diversification_yougov$race=="Hispanic"]=0
diversification_yougov$white_nonwhite[diversification_yougov$race=="Middle Eastern"]=0
diversification_yougov$white_nonwhite[diversification_yougov$race=="Native American"]=0
diversification_yougov$white_nonwhite[diversification_yougov$race=="Other"]=0
diversification_yougov$white_nonwhite[diversification_yougov$race=="Two or more races"]=0
diversification_yougov$white_nonwhite[diversification_yougov$race=="White"]=1


diversification_yougov$reasonable_violence[diversification_yougov$Q19_1=="Strongly disagree"]=0
diversification_yougov$reasonable_violence[diversification_yougov$Q19_1=="Disagree"]=1/4
diversification_yougov$reasonable_violence[diversification_yougov$Q19_1=="Neither agree nor disagree"]=2/4
diversification_yougov$reasonable_violence[diversification_yougov$Q19_1=="Agree"]=3/4
diversification_yougov$reasonable_violence[diversification_yougov$Q19_1=="Strongly agree"]=1

diversification_yougov$IDEO[diversification_yougov$ideo5=="Very liberal"]=0
diversification_yougov$IDEO[diversification_yougov$ideo5=="Liberal"]=1/4
diversification_yougov$IDEO[diversification_yougov$ideo5=="Moderate"]=2/4
diversification_yougov$IDEO[diversification_yougov$ideo5=="Conservative"]=3/4
diversification_yougov$IDEO[diversification_yougov$ideo5=="Very conservative"]=1

diversification_yougov$Blacks_less_than_they_deserve_reverse_coded[diversification_yougov$racialresent_1=="Strongly disagree"]=1
diversification_yougov$Blacks_less_than_they_deserve_reverse_coded[diversification_yougov$racialresent_1=="Disagree"]=3/4
diversification_yougov$Blacks_less_than_they_deserve_reverse_coded[diversification_yougov$racialresent_1=="Neither agree nor disagree"]=2/4
diversification_yougov$Blacks_less_than_they_deserve_reverse_coded[diversification_yougov$racialresent_1=="Agree"]=1/4
diversification_yougov$Blacks_less_than_they_deserve_reverse_coded[diversification_yougov$racialresent_1=="Strongly agree"]=0

diversification_yougov$Blacks_overcome_without_favors[diversification_yougov$racialresent_2=="Strongly disagree"]=0
diversification_yougov$Blacks_overcome_without_favors[diversification_yougov$racialresent_2=="Disagree"]=1/4
diversification_yougov$Blacks_overcome_without_favors[diversification_yougov$racialresent_2=="Neither agree nor disagree"]=2/4
diversification_yougov$Blacks_overcome_without_favors[diversification_yougov$racialresent_2=="Agree"]=3/4
diversification_yougov$Blacks_overcome_without_favors[diversification_yougov$racialresent_2=="Strongly agree"]=1

diversification_yougov$Black_not_trying_hard_enough[diversification_yougov$racialresent_3=="Strongly disagree"]=0
diversification_yougov$Black_not_trying_hard_enough[diversification_yougov$racialresent_3=="Disagree"]=1/4
diversification_yougov$Black_not_trying_hard_enough[diversification_yougov$racialresent_3=="Neither agree nor disagree"]=2/4
diversification_yougov$Black_not_trying_hard_enough[diversification_yougov$racialresent_3=="Agree"]=3/4
diversification_yougov$Black_not_trying_hard_enough[diversification_yougov$racialresent_3=="Strongly agree"]=1

diversification_yougov$generation_of_slavery_made_hard_reverse_coded[diversification_yougov$racialresent_4=="Strongly disagree"]=1
diversification_yougov$generation_of_slavery_made_hard_reverse_coded[diversification_yougov$racialresent_4=="Disagree"]=2/4
diversification_yougov$generation_of_slavery_made_hard_reverse_coded[diversification_yougov$racialresent_4=="Neither agree nor disagree"]=2/4
diversification_yougov$generation_of_slavery_made_hard_reverse_coded[diversification_yougov$racialresent_4=="Agree"]=1/4
diversification_yougov$generation_of_slavery_made_hard_reverse_coded[diversification_yougov$racialresent_4=="Strongly agree"]=0

diversification_yougov$Racial_resentment_scale <- (
  diversification_yougov$Blacks_less_than_they_deserve_reverse_coded + 
  diversification_yougov$Blacks_overcome_without_favors + 
  diversification_yougov$Black_not_trying_hard_enough + 
  diversification_yougov$generation_of_slavery_made_hard_reverse_coded
) / 4
  
diversification_yougov$AGE <- scales::rescale(2023 - diversification_yougov$birthyr)
diversification_yougov_white <- filter(diversification_yougov, white_nonwhite == 1)


# Analyses ----------------------------------------------------------------




p1a <- norc_diversification |>
  mutate(treated = factor(demographic_prime, levels = 0:1, labels = c("Control", "Treatment"), ordered=T)) |>
  group_by(treated) |>
  summarize(
    y = mean(reasonable_violence, na.rm = T),
    se = sd(reasonable_violence, na.rm = T) / sqrt(sum(!is.na(reasonable_violence))),
    ymin = y - 1.96 * se,
    ymax = y + 1.96 * se,
  ) |>
  ggplot(aes(x=treated, y=y, ymin=ymin, ymax=ymax, fill = treated)) +
  geom_col() +
  geom_linerange() +
  theme_bw() +
  scale_fill_brewer(type="qual", palette = 7, guide = NULL) +
  labs(
    x ="",
    y = "Support for Violence",
    title = "Average Level of Support for Violence",
    subtitle = "NORC",
    fill = "",
  )

p1b <- chronic_racial_threat_lucid |>
  filter(!is.na(violence_against_opposition_post)) |>
  filter(!is.na(demo_prime)) |>
  mutate(treated = factor(demo_prime, levels = 0:1, labels = c("Control", "Treatment"), ordered=T)) |>
  group_by(treated) |>
  summarize(
    y = mean(violence_against_opposition_post, na.rm = T),
    se = sd(violence_against_opposition_post, na.rm = T) / sqrt(sum(!is.na(violence_against_opposition_post))),
    ymin = y - 1.96 * se,
    ymax = y + 1.96 * se,
  ) |>
  ggplot(aes(x=treated, y=y, ymin=ymin, ymax=ymax, fill = treated)) +
  geom_col() +
  geom_linerange() +
  theme_bw() +
  scale_fill_brewer(type="qual", palette = 7, guide = NULL) +
  labs(
    x ="",
    y = "Support for Violence",
    title = "Average Level of Support for Violence",
    subtitle = "Lucid",
    fill = "",
  )

p1 <- p1a + p1b

ggsave("results/figure1.pdf", p1, height=4, width=8)
ggsave("results/figure1.png", p1, height=4, width=8, dpi=450)


p2a <- norc_diversification_whites |>
  mutate(treated = factor(demographic_prime, levels = 0:1, labels = c("Control", "Treatment"), ordered=T)) |>
  group_by(treated) |>
  summarize(
    y = mean(reasonable_violence, na.rm = T),
    se = sd(reasonable_violence, na.rm = T) / sqrt(sum(!is.na(reasonable_violence))),
    ymin = y - 1.96 * se,
    ymax = y + 1.96 * se,
  ) |>
  ggplot(aes(x=treated, y=y, ymin=ymin, ymax=ymax, fill = treated)) +
  geom_col() +
  geom_linerange() +
  theme_bw() +
  scale_fill_brewer(type="qual", palette = 7, guide = NULL) +
  labs(
    x ="",
    y = "Support for Violence",
    title = "Average Level of Support for Violence",
    subtitle = "NORC, subset to White respondents only",
    fill = "",
  )
p2b <- chronic_threat_LU_white |>
  filter(!is.na(violence_against_opposition_post)) |>
  filter(!is.na(demo_prime)) |>
  mutate(treated = factor(demo_prime, levels = 0:1, labels = c("Control", "Treatment"), ordered=T)) |>
  group_by(treated) |>
  summarize(
    y = mean(violence_against_opposition_post, na.rm = T),
    se = sd(violence_against_opposition_post, na.rm = T) / sqrt(sum(!is.na(violence_against_opposition_post))),
    ymin = y - 1.96 * se,
    ymax = y + 1.96 * se,
  ) |>
  ggplot(aes(x=treated, y=y, ymin=ymin, ymax=ymax, fill = treated)) +
  geom_col() +
  geom_linerange() +
  theme_bw() +
  scale_fill_brewer(type="qual", palette = 7, guide = NULL) +
  labs(
    x ="",
    y = "Support for Violence",
    title = "Average Level of Support for Violence",
    subtitle = "Lucid, subset to White respondents only",
    fill = "",
  )
p2 <- p2a + p2b

ggsave("results/figure2.pdf", p2, height=4, width=8)
ggsave("results/figure2.png", p2, height=4, width=8, dpi=450)


#### TESTING H1 
### DIFFERENCE IN MEANS 
### NORC
t.test(as.numeric(reasonable_violence) ~ as.numeric(demographic_prime), data = norc_diversification)
t.test(as.numeric(reasonable_violence) ~ as.numeric(demographic_prime), data = norc_diversification_whites)

norc_reg_NORC_study1_violence<- lm(reasonable_violence ~ demographic_prime +GENDER  + PID 
                                            + EDU + SOUTH , data = norc_diversification_whites)
summary(norc_reg_NORC_study1_violence)

COEF_RENAMER <- c(
  "demographic_prime" = "Treatment",
  "diversification_treatment" = "Treatment",
  "demo_prime" = "Treatment",
  "GENDER" = "Female",
  "PID" = "Party ID",
  "EDU" = "Education",
  "SOUTH" = "South",
  "violence_against_opposition_pre" = "Pre-Treatment Response",
  "IDEO" = "Ideology",
  "REGIONNortheast" = "Northeast",
  "REGIONWest" = "West",
  "REGIONSouth" = "South",
  "AGE" = "Age",
  "FEMALE" = "Female",
  "slavery_made_hard_for_Blacks_reverse_pre" = "Pre-Treatment Response"
)

modelsummary(
  norc_reg_NORC_study1_violence,
  stars = TRUE,
  fmt = 2,
  coef_rename = COEF_RENAMER,
  title = "Reasonable to use violence, NORC, Whites only",
  output = "results/tables/reasonable_violence_norc.docx"
)

### LUCID STUDY 2, DIFFERENCE IN MEANS
t.test(as.numeric(violence_against_opposition_post) ~ as.numeric(demo_prime), data = chronic_racial_threat_lucid)
t.test(as.numeric(violence_against_opposition_post) ~ as.numeric(demo_prime), data = chronic_threat_LU_white)








######
#### FIGURE 3 
####
reg_figure3_study2_VIOLENCE<-  lm(violence_against_opposition_post~demo_prime + violence_against_opposition_pre +
               PID + IDEO + EDU + REGION  + AGE + FEMALE,
             data = chronic_threat_LU_white) 
summary(reg_figure3_study2_VIOLENCE)


p3 <- reg_figure3_study2_VIOLENCE %>% 
  broom::tidy() %>% 
  filter(term %in% c("demo_prime", "violence_against_opposition_pre")) %>%
  mutate(term = case_when(
    term == "demo_prime" ~ "Treatment", 
    term == "violence_against_opposition_pre" ~ "Support for Violence\n(Pre-Treatment)")) %>%
  ggplot(aes(y = term, x = estimate)) + 
  geom_vline(xintercept = 0, linetype = 2) + 
  geom_point() + 
  geom_pointrange(aes(xmin = estimate-(1.96*std.error), xmax = estimate+(1.96*std.error)), fatten = 1.5) + 
  labs(
    x = "Effect on Support for Violence (post-treatment)",
    y = "",
    title = "Violence Model",
    #subtitle = "Generations of slavery and discrimation have created conditions that make it\ndifficult for blacks to work their way out of the lower class.\n\nLucid, White respondents only",
  ) +
  theme_minimal() 

ggsave("results/figure3.pdf", p3, height = 4, width = 6)
ggsave("results/figure3.png", p3, height = 4, width = 6, dpi = 450)

modelsummary(
  reg_figure3_study2_VIOLENCE,
  stars = TRUE,
  fmt = 2,
  coef_rename = COEF_RENAMER,
  title = "Reasonable to use violence, Lucid, Whites only",
  output = "results/tables/reasonable_violence_lucid.docx"
)


######
###  FIGURE 5, Change to 4s
#####
reg_figure5_study2_RR<-  lm(slavery_made_hard_for_Blacks_reverse_post ~ demo_prime  + slavery_made_hard_for_Blacks_reverse_pre + 
                              PID + IDEO + EDU + REGION  + AGE + FEMALE,
                            data = chronic_threat_LU_white) 

summary(reg_figure5_study2_RR)

p5 <- reg_figure5_study2_RR %>% 
  broom::tidy() %>% 
  filter(term %in% c("demo_prime", "slavery_made_hard_for_Blacks_reverse_pre")) %>%
  mutate(term = case_when(
    term == "demo_prime" ~ "Treatment", 
    term == "slavery_made_hard_for_Blacks_reverse_pre" ~ "Racial Resentment\n(Pre-Treatment)")) %>%
  ggplot(aes(y = term, x = estimate)) + 
  geom_vline(xintercept = 0, linetype = 2) + 
  geom_point() + 
  geom_pointrange(aes(xmin = estimate-(1.96*std.error), xmax = estimate+(1.96*std.error)), fatten = 1.5) + 
  labs(
    x = "Effect on Racial Resentment (post-treatment)",
    y = "",
    title = "Racial Resentment Model",
    subtitle = "Generations of slavery and discrimation have created conditions that make it\ndifficult for blacks to work their way out of the lower class.\n\nLucid, White respondents only",
  ) +
  theme_minimal() 

ggsave("results/figure4.pdf", p5, height = 4, width = 6)
ggsave("results/figure4.png", p5, height = 4, width = 6, dpi = 450)

modelsummary(
  reg_figure5_study2_RR,
  stars = TRUE,
  fmt = 2,
  coef_rename = COEF_RENAMER,
  title = "Racial resentment (slavery item), Lucid, Whites only",
  output = "results/tables/racial_resentment_slavery_lucid.docx"
)


modelsummary(
  list(
    "Racial resentment" = reg_figure5_study2_RR,
    "Support for violence" = reg_figure3_study2_VIOLENCE
  ),
  stars = TRUE,
  fmt = 2,
  coef_rename = COEF_RENAMER,
  title = "Racial resentment and violence, Lucid, White respondents only",
  output = "results/tables/appendix_3_lucid.html"
)



#####
### OVERALL EFFECTS FOR THREAT, FIGURE 6
####
reg_checking_asian_threat_overall<-  lm(ASIAN_THREAT ~ demo_prime +
                                          PID + IDEO + EDU + REGION,
                                        data = chronic_threat_LU_white) 
summary(reg_checking_asian_threat_overall)

reg_checking_latinx_threat_overall<-  lm(LATINX_THREAT ~ demo_prime +
                                          PID + IDEO + EDU + REGION,
                                        data = chronic_threat_LU_white) 
summary(reg_checking_latinx_threat_overall)

reg_checking_black_threat_overall<-  lm(BLACK_THREAT ~ demo_prime +
                                  PID + IDEO + EDU + REGION,
                                data = chronic_threat_LU_white) 
summary(reg_checking_black_threat_overall)



modelsummary(
  list(
    "Asian threat" = reg_checking_asian_threat_overall,
    "Latinx threat" = reg_checking_latinx_threat_overall,
    "Black threat" = reg_checking_black_threat_overall
  ),
  stars = TRUE,
  fmt = 2,
  coef_rename = COEF_RENAMER,
  title = "Racial threat, Lucid, Whites only",
  output = "results/tables/racial_threat_lucid.docx"
)

######
###### FIGURE 6 
######
p6 <- plot_coefs(reg_checking_black_threat_overall,
           reg_checking_asian_threat_overall, 
           reg_checking_latinx_threat_overall,
           coefs = c("Diversification Treatment" = "demo_prime"),
           model.names = c("Black Threat", "Asian Threat",
                           "Latinx Threat"))

ggsave("results/figure5.pdf", p6, height = 5, width = 6)
ggsave("results/figure5.png", p6, height = 5, width = 6, dpi=450)


#####
###### YOUGOV, STUDY 3
#####

Yougov_study3_violence<-  lm(reasonable_violence ~ diversification_treatment +
                               PID + GENDER + EDU + IDEO + AGE,
                             data = diversification_yougov_white,
                             weights = weight)
summary(Yougov_study3_violence)

Yougov_study3_RR<-  lm(Racial_resentment_scale ~ diversification_treatment +
                               PID + GENDER + EDU + IDEO + AGE,
                             data = diversification_yougov_white,
                             weights = weight)
summary(Yougov_study3_RR)


modelsummary(
  list(
    "Violence" = Yougov_study3_violence,
    "Racial resentment" = Yougov_study3_RR
  ),
  stars = TRUE,
  fmt = 2,
  coef_rename = COEF_RENAMER,
  title = "YouGov, Whites only",
  output = "results/tables/models_yougov.docx"
)



